import shutil
import os

jobtxt = r"E:\python\test\实例\text2.txt"

# 创建一个临时文件路径
tmp_file_path = r"E:\python\test\实例\text_opt.txt"

# 打开原始文件和临时文件进行处理
with open(jobtxt, 'r', encoding='utf-8') as src_job, \
     open(tmp_file_path, 'w', encoding='utf-8') as tmp_file:
    for line in src_job:
        if "ssh" in line:
            new_line = line.replace("ssh", "shell")
            tmp_file.write(new_line)
        else:
            tmp_file.write(line)

# 使用文件路径进行复制操作
shutil.copy(tmp_file_path, jobtxt)
os.remove(tmp_file_path)